package com.lz.app.demo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.lz.app.demo.annotations.TokenRequired;
import com.lz.app.demo.dao.TblLanzuoUsersDao;
import com.lz.app.demo.domain.TblLanzuoUsers;
import com.lz.app.demo.dto.request.LoginRequestDto;
import com.lz.app.demo.dto.response.UserInfoResponseDto;
import com.lz.app.demo.dto.response.UserLoginResponseDto;
import com.lz.app.demo.jwt.Jwt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: UsersController
 * @ProjectName app-service
 * @Author huchao
 * @Date 2018/9/4上午10:53
 */
@RestController
@RequestMapping(value = "/tbllanzuousers")
@Slf4j
@Api(description = "用户")
public class UsersController {

	@Autowired
	private TblLanzuoUsersDao tblLanzuoUsersDao;

	@RequestMapping(value = "/jwt_zhizhi_login_p", method = RequestMethod.POST)
	@ApiOperation(value = "用户登录")
	public UserLoginResponseDto login(@RequestBody LoginRequestDto loginRequestDto) throws Exception {
		log.info("login requestDto:" + JSONObject.toJSONString(loginRequestDto));
		String username = loginRequestDto.getUsername();
		String password = loginRequestDto.getPassword();

		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			throw new Exception("用户名或密码为空");
		}

		TblLanzuoUsers tblLanzuoUsers = tblLanzuoUsersDao.findTblLanzuoUsersByUsername(username);
		if (tblLanzuoUsers == null) {
			throw new Exception("用户不存在");
		}
		if (!password.equals(tblLanzuoUsers.getPwd())) {
			throw new Exception("用户名或密码错误");
		}
		UserLoginResponseDto userLoginResponse = new UserLoginResponseDto();
		userLoginResponse.setDepartment(tblLanzuoUsers.getDepartment());
		userLoginResponse.setPhone(tblLanzuoUsers.getPhone());
		userLoginResponse.setRongUid(tblLanzuoUsers.getRemark1());
		userLoginResponse.setRongToken(tblLanzuoUsers.getRemark2());


		// 生成token
		Map<String, Object> payload = new HashMap<String, Object>();
		Date date = new Date();
		payload.put("id", tblLanzuoUsers.getId());
		payload.put("name", tblLanzuoUsers.getUsername());
		payload.put("iat", date.getTime());
		String token = Jwt.createToken(payload);
		userLoginResponse.setToken(token);
		return userLoginResponse;
	}


	@RequestMapping(value = "", method = RequestMethod.GET)
	@TokenRequired
	@ApiOperation(value = "用户列表")
	public List<UserInfoResponseDto> userList() {
		List<TblLanzuoUsers> tblLanzuoUsersList = tblLanzuoUsersDao.findAll();
		List<UserInfoResponseDto> userInfoResponseDtoList = new ArrayList<>();
		for (TblLanzuoUsers tblLanzuoUsers : tblLanzuoUsersList) {
			UserInfoResponseDto userInfoResponseDto = new UserInfoResponseDto();
			userInfoResponseDto.setDepartment(tblLanzuoUsers.getDepartment());
			userInfoResponseDto.setPhone(tblLanzuoUsers.getPhone());
			userInfoResponseDto.setId(tblLanzuoUsers.getId());
			userInfoResponseDto.setRongUid(tblLanzuoUsers.getRemark1());
			userInfoResponseDto.setRongToken(tblLanzuoUsers.getRemark2());
			userInfoResponseDto.setUsername(tblLanzuoUsers.getUsername());
			userInfoResponseDtoList.add(userInfoResponseDto);
		}
		return userInfoResponseDtoList;
	}

}
